package com.power.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.power.domain.Member;
import com.power.model.Result;
import com.power.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description 后台管理系统维护会员信息
 * @Author dingchunlin
 * @Date 2024/07/20 16:02
 * @Version 1.0.0
 */
@Api(tags = "后台管理系统会员接口管理")
@RestController
@RequestMapping("/admin/user")
public class SysMemberController {

    @Autowired
    private MemberService memberService;

    /**
     * 分页查询会员信息
     * @param current 页码
     * @param size 每页显示数量
     * @param nickName 昵称
     * @param status 状态
     * @return 分页查询结果
     */
    @ApiOperation(value = "多条件分页查询会员信息")
    @GetMapping("/page")
    @PreAuthorize("hasAnyAuthority('admin:user:page')")
    public Result<Page<Member>> loadMemberPage(@RequestParam Long current,
                                                @RequestParam Long size,
                                                @RequestParam(required = false) String nickName,
                                                @RequestParam(required = false) Integer status) {
        // 创建分页对象
        Page<Member> page = new Page<>(current, size);
        // 调用service查询会员信息
        page = memberService.page(page,new LambdaQueryWrapper<Member>()
                .eq(ObjectUtil.isNotEmpty(status),Member::getStatus,status)
                .like(StringUtils.hasText(nickName),Member::getNickName,nickName)
                .orderByDesc(Member::getCreateTime));
        return Result.success(page);
    }

    /**
     * 根据标识查询会员信息
     * @param id 会员标识
     * @return 会员信息
     */
    @ApiOperation(value = "根据标识查询会员信息")
    @GetMapping("/info/{id}")
    @PreAuthorize("hasAnyAuthority('admin:user:info')")
    public Result<Member> loadMemberInfo(@PathVariable Long id) {
        Member member = memberService.getOne(new LambdaQueryWrapper<Member>()
                .select(Member::getId,Member::getOpenId,Member::getPic,Member::getNickName,Member::getStatus)
                .eq(Member::getId,id));
        return Result.success(member);
    }

    /**
     * 更新会员状态
     * @param member 会员信息
     * @return 更新结果
     */
    @ApiOperation(value = "更新会员状态")
    @PutMapping
    @PreAuthorize("hasAnyAuthority('admin:user:update')")
    public Result<String> modifyMemberStatus(@RequestBody Member member) {
        member.setUpdateTime(new Date());
        Boolean modify = memberService.updateById(member);
        return Result.handle(modify);
    }

    /**
     * 批量删除会员信息
     * @param ids 会员标识列表
     * @return 删除结果
     */
    @ApiOperation(value = "批量删除会员信息")
    @DeleteMapping
    @PreAuthorize("hasAnyAuthority('admin:user:delete')")
    public Result<String> removeMembers(@RequestBody List<Integer> ids) {
        // 创建会员集合对象
        List<Member> memberList = new ArrayList<>();
        // 遍历ids列表，将id封装到Member对象中，添加到集合中
        ids.forEach(id -> {
            Member member = new Member();
            member.setId(id);
            member.setStatus(-1);
            memberList.add(member);
        });
        // 调用service批量删除会员信息
        Boolean remove = memberService.updateBatchById(memberList);
        return Result.handle(remove);
    }


    /**
     * 根据openId获取昵称
     * @param openId openId
     * @return 昵称
     */
    @GetMapping("/getNickNameByOpenId")
    Result<String> getNickNameByOpenId(@RequestParam String openId){
        Member member = memberService.getOne(new LambdaQueryWrapper<Member>()
               .select(Member::getNickName)
               .eq(Member::getOpenId,openId));
        return Result.success(member.getNickName());
    }

}
